package com.wisemoresystem.seal.action.workdate;

import java.sql.Time;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wisemoresystem.seal.base.action.ProjBaseAction;
import com.wisemoresystem.seal.base.tip.Tip;
import com.wisemoresystem.seal.model.t_bank;
import com.wisemoresystem.seal.model.t_mac_wktime_special;
import com.wisemoresystem.seal.service.BankService;
import com.wisemoresystem.seal.service.WorkdateService;
import com.wisemoresystem.seal.util.json.JsonTools;
import com.wisemoresystem.seal.web.WebTools;

/**
 * 机构工作时间设置
 * 
 * @author Administrator
 *
 */
@Controller("workdate")
@Scope("prototype")
@RequestMapping("/ix/seal/workdate")
public class WorkdateAction extends ProjBaseAction {
	@Autowired
	private WorkdateService workdateservice;
	@Autowired
	private BankService bService;

	/**
	 * 机构工作时间列表
	 * 
	 * @param bank_id
	 *            机构id
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(String bank_id) {

		List<Map<String, Object>> bankList = bService.selectUserBank();
		String bankJson = JsonTools.toJson(bankList);
		List<Map<String, Object>> list = workdateservice
				.selectmac_wktime(bank_id);

		ModelAndView mv = new ModelAndView("/jsp/workdate/list");
		mv.addObject("list", list);
		mv.addObject("bankJson", bankJson);
		mv.addObject("bank_id", bank_id);
		return mv;
	}

	/**
	 * 添加时间的机构信息
	 * 
	 * @param bank_id
	 * @return
	 */
	@RequestMapping("/add_form")
	public ModelAndView add_form(String bank_id) {

		t_bank bank = workdateservice.findObjById(bank_id, t_bank.class);

		ModelAndView mv = new ModelAndView("/jsp/workdate/add_form");
		mv.addObject("bank", bank);
		return mv;
	}

	/**
	 * 机构工作时间添加
	 * 
	 * @param wk1
	 *            周一开始时间
	 * @param wk1e
	 *            周一结束时间
	 * @param wk2
	 * @param wk2e
	 * @param wk3
	 * @param wk3e
	 * @param wk4
	 * @param wk4e
	 * @param wk5
	 * @param wk5e
	 * @param wk6
	 * @param wk6e
	 * @param wk7
	 * @param wk7e
	 * @param bank_id
	 * @return
	 */
	@RequestMapping("/add")
	public ModelAndView add(String wk1, String wk1e, String wk2, String wk2e,
			String wk3, String wk3e, String wk4, String wk4e, String wk5,
			String wk5e, String wk6, String wk6e, String wk7, String wk7e,
			String bank_id) {

		t_mac_wktime_special mac = new t_mac_wktime_special();
		mac.setUser_id(WebTools.getCurrentUserId());
		mac.setBank_id(bank_id);
		mac.setWk1(Time.valueOf(wk1));
		mac.setWk1e(Time.valueOf(wk1e));
		mac.setWk2(Time.valueOf(wk2));
		mac.setWk2e(Time.valueOf(wk2e));
		mac.setWk3(Time.valueOf(wk3));
		mac.setWk3e(Time.valueOf(wk3e));
		mac.setWk4(Time.valueOf(wk4));
		mac.setWk4e(Time.valueOf(wk4e));
		mac.setWk5(Time.valueOf(wk5));
		mac.setWk5e(Time.valueOf(wk5e));
		mac.setWk6(Time.valueOf(wk6));
		mac.setWk6e(Time.valueOf(wk6e));
		mac.setWk7(Time.valueOf(wk7));
		mac.setWk7e(Time.valueOf(wk7e));
		workdateservice.saveObj(mac);
		logT.logH("添加印控仪工作时间", t_mac_wktime_special.class, mac.get_id());

		super.setTipMsg("保存成功！", Tip.Type.success);
		ModelAndView mv = new ModelAndView("redirect:list.htm");
		mv.addObject("bank_id", bank_id);
		return mv;
	}

	/**
	 * 要修改的机构工作时间
	 * 
	 * @param mac_wktime_special_id
	 *            特殊工作时间id
	 * @return
	 */

	@RequestMapping("/edit_form")
	public ModelAndView edit_form(String mac_wktime_special_id) {

		t_mac_wktime_special mac_wktime_special = (t_mac_wktime_special) oc
				.findObjById(mac_wktime_special_id, t_mac_wktime_special.class);
		t_bank bank = workdateservice.findObjById(
				mac_wktime_special.getBank_id(), t_bank.class);

		ModelAndView mv = new ModelAndView("/jsp/workdate/edit_form");
		mv.addObject("mac_wktime_special", mac_wktime_special);
		mv.addObject("bank", bank);
		return mv;
	}

	/**
	 * 机构工作时间修改
	 * 
	 * @param mac_wktime_special_id
	 *            特殊工作时间id
	 * @param wk1
	 *            周一开始时间
	 * @param wk1e
	 *            周一结束时间 下边依次类推
	 * @param wk2
	 * @param wk2e
	 * @param wk3
	 * @param wk3e
	 * @param wk4
	 * @param wk4e
	 * @param wk5
	 * @param wk5e
	 * @param wk6
	 * @param wk6e
	 * @param wk7
	 * @param wk7e
	 * @return
	 */
	@RequestMapping("/edit")
	public ModelAndView edit(String mac_wktime_special_id, String wk1,
			String wk1e, String wk2, String wk2e, String wk3, String wk3e,
			String wk4, String wk4e, String wk5, String wk5e, String wk6,
			String wk6e, String wk7, String wk7e) {

		t_mac_wktime_special mac = (t_mac_wktime_special) oc.findObjById(
				mac_wktime_special_id, t_mac_wktime_special.class);

		mac.setWk1(Time.valueOf(wk1));
		mac.setWk1e(Time.valueOf(wk1e));
		mac.setWk2(Time.valueOf(wk2));
		mac.setWk2e(Time.valueOf(wk2e));
		mac.setWk3(Time.valueOf(wk3));
		mac.setWk3e(Time.valueOf(wk3e));
		mac.setWk4(Time.valueOf(wk4));
		mac.setWk4e(Time.valueOf(wk4e));
		mac.setWk5(Time.valueOf(wk5));
		mac.setWk5e(Time.valueOf(wk5e));
		mac.setWk6(Time.valueOf(wk6));
		mac.setWk6e(Time.valueOf(wk6e));
		mac.setWk7(Time.valueOf(wk7));
		mac.setWk7e(Time.valueOf(wk7e));
		workdateservice.updateObj(mac);

		logT.logH("修改印控仪工作时间", t_mac_wktime_special.class, mac.get_id());

		super.setTipMsg("修改成功！", Tip.Type.success);
		ModelAndView mv = new ModelAndView("redirect:list.htm");
		mv.addObject("bank_id", mac.getBank_id());
		return mv;
	}

	/**
	 * 删除工作时间
	 * 
	 * @param mac_wktime_special_id
	 *            特殊工作时间id
	 * @param bank_id
	 *            机构id
	 * @return
	 */
	@RequestMapping("/del")
	public ModelAndView del(String mac_wktime_special_id, String bank_id) {

		workdateservice.delObjById(mac_wktime_special_id,
				t_mac_wktime_special.class);
		logT.logH("删除印控仪工作时间", t_mac_wktime_special.class,
				mac_wktime_special_id);

		super.setTipMsg("删除成功！", Tip.Type.success);
		ModelAndView mv = new ModelAndView("redirect:list.htm");
		mv.addObject("bank_id", bank_id);
		return mv;
	}
}
